package com.xiaocg.completeFuture;

import java.io.BufferedWriter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

public class CompleteFutureTest {

    private static List<Stu> stus = Arrays.asList(
            new Stu("taobao"),
            new Stu("jd"),
            new Stu("pdd"),
            new Stu("dangdang")
    );

    public static void main(String[] args) {

        long start = System.currentTimeMillis();

        List<String> collect = stus.stream().map(stu -> CompletableFuture.supplyAsync(() -> {
            return stu.getName() + "的年龄是" + stu.getAge();
        })).toList().stream().map(CompletableFuture::join).toList();

        collect.forEach(System.out::println);

        System.out.println("执行时间:"+(System.currentTimeMillis()-start));

        test();

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static  void test(){
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            int i = ThreadLocalRandom.current().nextInt(10);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return i;
        }).whenComplete((v,e)->{
            System.out.println("test:"+v);
        }).exceptionally(throwable -> {
            System.out.println(throwable.getMessage());
            return null;
        });
    }

    public static  void test1(){
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            int i = ThreadLocalRandom.current().nextInt(10);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return i;
        }).thenApply(v->{
            System.out.println("test2:"+v);
            return v+ThreadLocalRandom.current().nextInt(10);
        }).thenApply(v->{
            System.out.println("test3:"+v);
            return v+ThreadLocalRandom.current().nextInt(10);
        }).whenComplete((v,e)->{
            System.out.println("test:"+v);
        }).exceptionally(throwable -> {
            System.out.println(throwable.getMessage());
            return null;
        });
    }

    public static  void test2(){
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            int i = ThreadLocalRandom.current().nextInt(10);
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return i;
        }).handle((v,e)->{
            System.out.println("test2:"+v);
            return v+ThreadLocalRandom.current().nextInt(10);
        }).handle((v,e)->{
            System.out.println("test3:"+v);
            return v+ThreadLocalRandom.current().nextInt(10);
        }).whenComplete((v,e)->{
            System.out.println("test:"+v);
        }).exceptionally(throwable -> {
            System.out.println(throwable.getMessage());
            return null;
        });
    }

}
